home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / SRS / client / src / config.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-12  |  5.6 KB  |  267 lines

  1. /* This has functions to read the configuration file */
  2. /* ------------------------------------------------- */
  3.  
  4. #include "headers.h" /* has all important stuff */
  5.  
  6. /* reads configure file.. etc. */
  7. void config()
  8. {
  9. #  if !defined(SUN) && !defined(BSD)
  10.    int res1;
  11. #  endif
  12.  
  13.    int dosend = 0;
  14.  
  15.    char *res; 
  16.    char buf[MAXREADSIZE/4];
  17.  
  18.    if (confd != NULL)
  19.    {
  20.       register int i;
  21.  
  22.       (void)fclose(confd);
  23.  
  24.       if (debugging == 1)
  25.       {
  26.          (void)putchar('\n');
  27.          (void)write(dblogfd, "\n", 1);
  28.       }
  29.  
  30.       debug("got request to re-HUP config file\n\n");
  31.  
  32.       for (i = 0; i <= curlogfd; i++) 
  33.       {
  34.           if ((logs[i].fd <= 0) && (logs[i].filename == NULL)) continue;
  35.           else
  36.           {
  37.              if (logs[i].fd > 0) close(logs[i].fd);
  38.              if (logs[i].filename != NULL) free(logs[i].filename);
  39.  
  40.              memset(&logs[i], 0, sizeof(struct log));
  41.  
  42.              logs[i].fd = ERROR;
  43.              logs[i].filename = NULL;
  44.           }
  45.       }
  46.  
  47.       if ((connected == 1) && (gotInfoServ == 1)) dosend = 1;
  48.    }
  49.  
  50. # if !defined(SUN) && !defined(BSD)
  51. #  ifdef _POSIX_SAVED_IDS
  52.    res1 = setuid(0);
  53. #  else
  54.    res1 = seteuid(0);
  55. #  endif
  56.  
  57.    if (res1 == ERROR)
  58.    {
  59.       error("error setting [e]uid: %s\n\n", strerror(errno));
  60.       quit(ERROR);
  61.    }
  62. # endif
  63.  
  64.    confd = fopen(CONFILE, "r");
  65.    if (confd == NULL)
  66.    {
  67.       error("error opening %s: %s\n\n", CONFILE, strerror(errno));
  68.       quit(ERROR);
  69.    }
  70.     
  71.    curlogfd = ERROR;
  72.  
  73.    while(1)
  74.    {      
  75.       memset(buf, 0, sizeof(buf));
  76.  
  77.       res = fgets(buf, sizeof(buf)-1, confd);
  78.       if (res == NULL)
  79.       {
  80.          if (errno == EINTR) continue;
  81.          else if ((!feof(confd)) && (errno > 0))
  82.          {
  83.             error("error reading from %s: %s\n\n", CONFILE, strerror(errno));
  84.             quit(ERROR);
  85.          }
  86.  
  87.          else
  88.          {
  89.             /* (void)fclose(confd); */ /* may need to re-HUP later on */
  90.             break;
  91.          }
  92.       }    
  93.  
  94.       if (isprint((int)buf[0]) == 0) continue;
  95.       else
  96.       {
  97.          debug("parsing line:\n%s\n", buf);
  98.          parseSysConf(buf);          
  99.       }
  100.    }
  101.  
  102. #  if !defined(SUN) && !defined(BSD)
  103.    if (pwd != NULL)
  104.    {
  105. #     ifdef _POSIX_SAVED_IDS
  106.       res1 = setuid(pwd->pw_uid);
  107. #     else
  108.       res1 = seteuid(pwd->pw_uid);
  109. #     endif
  110.  
  111.       if (res1 == ERROR)
  112.       {
  113.          error("error setting [e]uid: %s\n\n", strerror(errno));
  114.          quit(ERROR);
  115.       }
  116.    }
  117. #  endif
  118.  
  119.    printConfig();
  120.  
  121.    if (dosend == 1)
  122.    {
  123.       debug("now re-sending syslog.conf to server\n");
  124.       sendSysConf();
  125.    }
  126. }
  127.  
  128.  
  129. /* -------------------------------- */
  130.  
  131.  
  132. /* output what's being logged where */
  133. void printConfig()
  134. {
  135.    register int i;
  136.  
  137.    debug("current files being logged to:\n");
  138.  
  139.    for (i = 0; i <= curlogfd; i++)
  140.    {
  141.        if ((logs[i].filename == NULL) || (logs[i].fd <= 0)) continue;
  142.  
  143.        if ((logs[i].facility != ALL) && 
  144.            (logs[i].priority.priority != ALL)  &&
  145.            (logs[i].priority.priority != NONE))
  146.        {
  147.           debug("%s, for %s.%s\n", logs[i].filename,
  148.                 facValToName(logs[i].facility),
  149.                 priValToName(logs[i].priority.priority));
  150.  
  151.        }
  152.  
  153.        else
  154.        {
  155.           if (logs[i].facility == ALL)
  156.           {
  157.              if (logs[i].priority.priority == ALL)
  158.                 debug("%s, for *.*\n", logs[i].filename);
  159.  
  160.              else if (logs[i].priority.priority == NONE)
  161.                 debug("%s, for *.none\n", logs[i].filename);
  162.  
  163.              else
  164.                 debug("%s, for *.%s\n", logs[i].filename, 
  165.                       priValToName(logs[i].priority.priority));
  166.           }
  167.  
  168.           else if (logs[i].priority.priority == ALL)
  169.              debug("%s, for %s.*\n", logs[i].filename,
  170.                    facValToName(logs[i].facility));
  171.  
  172.           else if (logs[i].priority.priority == NONE)
  173.              debug("%s, for %s.none\n", logs[i].filename,
  174.                    facValToName(logs[i].facility));
  175.        }
  176.    }
  177.  
  178.    if (debugging == 1)
  179.    {
  180.       (void)putchar('\n');
  181.       (void)write(dblogfd, "\n", 1);
  182.    }
  183. }
  184.  
  185.  
  186. /* -------------------------------- */
  187.  
  188.  
  189. /* send syslog.conf to server */
  190. void sendSysConf()
  191. {
  192.    int res;
  193.    char *res1;
  194.    char buf[MAXREADSIZE];
  195.  
  196.    send_data("START SYSLOG.CONF\n");
  197.  
  198.    /* FIX - add timeout */
  199.    while(1)
  200.    {
  201.       memset(buf, 0, sizeof(buf));
  202.       recv_data(buf, sizeof(buf));
  203.  
  204.       if (strncmp(buf, "START SYSLOG.CONF OKAY", 22) == 0)
  205.       {
  206.          debug("got server's permissions to start sending\n\n");
  207.          break;
  208.       }
  209.    }
  210.  
  211. /* (should already be open)
  212.    confd = fopen(CONFILE, "r");
  213.    if (confd == NULL)
  214.    {
  215.       error("error opening %s: %s\n\n", CONFILE, strerror(errno));
  216.       quit(ERROR);
  217.    }
  218. */
  219.     
  220.    res = fseek(confd, 0, SEEK_SET);
  221.    if (res == ERROR)
  222.    {
  223.       error("error with fseek(): %s\n\n", strerror(errno));
  224.       quit(ERROR);
  225.    }
  226.  
  227.    while(1)
  228.    {      
  229.       memset(buf, 0, sizeof(buf));
  230.  
  231.       res1 = fgets(buf, sizeof(buf)-1, confd);
  232.       if (res1 == NULL)
  233.       {
  234.          if (errno == EINTR) continue;
  235.          else if ((!feof(confd)) && (errno > 0))
  236.          {
  237.             error("error reading from %s: %s\n\n", CONFILE, strerror(errno));
  238.             quit(ERROR);
  239.          }
  240.  
  241.          else 
  242.          {
  243.             /* (void)fclose(confd); */ /* may need to re-HUP later on */
  244.             break;
  245.          }
  246.       }    
  247.  
  248.       if (isprint((int)buf[0]) == 0) continue;
  249.       else send_data(buf);
  250.    }
  251.  
  252.    send_data("END SYSLOG.CONF\n");
  253.  
  254.    /* FIX - add timeout */
  255.    while(1)
  256.    {
  257.       memset(buf, 0, sizeof(buf));
  258.       recv_data(buf, sizeof(buf));
  259.  
  260.       if (strncmp(buf, "SUCCESSFUL logging", 18) == 0)
  261.       {
  262.          (void)printf("syslog.conf was sent successfully\n\n");
  263.          break;
  264.       }
  265.    }
  266. }
  267.